package com.sa.web.front.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.StuInfoBean;
import com.sa.web.bean.UserBean;
import com.sa.web.dao.service.StuInfoDao;
import com.sa.web.dao.service.UserDao;
import com.sa.web.util.Util;

/**
 * 后台管理首页
 */

public class UserAction extends ActionSupport{
	/**序列*/ 
	private static final long serialVersionUID = 1L;
	
	private UserBean userBean;
	
	private UserDao userDao;
	
	private StuInfoBean stuInfo;
	
	private StuInfoDao stuInfoDao;
	
	private String checkword;
	
	public String init() throws Exception{	
		return "success";
	}
	
	
	public void validateUserLogin() throws Exception{
		this.clearFieldErrors();
		if(Util.isEmpty(userBean.getUsername())){
			this.addFieldError("username", "请输入用户名");
		}
		
		if(Util.isEmpty(userBean.getPassword())){
			this.addFieldError("password", "请输入密码");
		}
		
		if(Util.isEmpty(checkword)){
			this.addFieldError("checkword", "请输入验证码");
		}else{
			ActionContext context = ActionContext.getContext();  
			Map<String , Object> session = context.getSession();
			String sessionCheckWord = (String)session.get("RANDOMVALIDATECODEKEY");
			if(!sessionCheckWord.equalsIgnoreCase(checkword)){
				this.addFieldError("checkword", "验证码不正确");
			}
		}
		
	}
	
	public String userLogin() throws Exception{
		UserBean user = userDao.login(userBean);
		if(user != null){
			stuInfo = stuInfoDao.getStuInfoByUser(user.getUsername());
			 ActionContext context = ActionContext.getContext();  
			 Map<String , Object> session = context.getSession();
			 session.put("USER", user);
			 return LOGIN;
		}
		return INPUT;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public StuInfoBean getStuInfo() {
		return stuInfo;
	}

	public void setStuInfo(StuInfoBean stuInfo) {
		this.stuInfo = stuInfo;
	}

	public StuInfoDao getStuInfoDao() {
		return stuInfoDao;
	}

	public void setStuInfoDao(StuInfoDao stuInfoDao) {
		this.stuInfoDao = stuInfoDao;
	}


	public String getCheckword() {
		return checkword;
	}


	public void setCheckword(String checkword) {
		this.checkword = checkword;
	}

}
